<template>
  <!--导入导出按钮  -->

  <div style="margin: 20px;">
    <el-row>
      <el-col :span="4">
        <el-upload
            ref="uploadCatesRef"
            class="upload-demo"
            :action="UploadPath"
            :on-success="handleUploadSuccess"
            :on-error="handleUploadError"
            :headers="{Authorization: getToken()}"
            :limit=" 1"
        >
          <el-button type="warning" :icon="Upload">导入</el-button>
          <template #tip>
            <div class="el-upload__tip">
              csv/xls/xlsx 文件必须小于100MB.
            </div>
          </template>
        </el-upload>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" :icon="Download" @click="handleExport">导出</el-button>
      </el-col>
    </el-row>
  </div>
  <!--数据表格-->
  <div>
    <el-table
        :data="categories"
        style="width: 100%"
        row-key="id"
        border
        lazy
        :load="load"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="分类名称"/>
      <el-table-column prop="imageUrl" label="图标">
        <template #default="scope">
          <img v-if="scope.row.imageUrl" :src="scope.row.imageUrl" alt="" style="width: 40px;height: 40px">
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序"/>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'warning'"
                  v-text="scope.row.status === 1 ? '正常' : '不显示'"></el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"/>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleShowEditDialog(scope.row)">
            修改
          </el-button>
          <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>

import {Download, Upload} from "@element-plus/icons-vue";
import {listByPid, delCategory} from "@/api/product/category.js";
import {onMounted} from "vue";

const {proxy} = getCurrentInstance();
import {ElMessage} from "element-plus";
import {getToken} from "@/utils/auth";

//用来绑定页面中上传组件中的文件列表
const uploadCatesRef = ref({})

//上传成功和失败的回调
const handleUploadSuccess = (response) => {
  //响应报文状态码200时回调
  if (+response.code === 200) {
    ElMessage.success("导入成功")
  } else {
    ElMessage.error(response.msg)
  }
  //导入后无论成功还是失败  都清空导入组件中的文件列表
  uploadCatesRef.value.clearFiles()
}

const handleUploadError = (err, file) => {
  ElMessage.error("导入失败")
}
//导入相关属性
const UploadPath = "http://localhost:8080/product/category/import"
//页面初始化 vue的回调函数
onMounted(() => {
  //查询第一级分类数据
  initData(0)
})
//数据列表
const categories = ref([])
//加载分页列表的函数
const initData = async (pid) => {
  const {data} = await listByPid(pid)
  //categories表示一级的分类列表
  categories.value = data
}

//懒加载函数
const load = async (row, treeNode, resolve) => {
  //row：表示触发懒加载数据行数

  //获取row下一级的数据
  const {data} = await listByPid(row.id)
  //resolve：代表解析下一级数据的对象
  resolve(data)
}

//删除分类
const handleDelete = async (row) => {
  //删除分类
  await delCategory(row.id)
  //重新加载数据
  // initData(0)
  location.reload()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("product/category/export", {}, `cates_${new Date().getTime()}.xlsx`);
}
</script>

<style scoped>

</style>